package com.xnxy.design.action;

import com.xnxy.design.model.Announcement;
import com.xnxy.design.service.AnnouncementService;
import com.xnxy.design.service.CoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class AnnouncementAction extends CoreController<Announcement,Integer>{

    @Autowired
    AnnouncementService announcementService;

    @Override
    public CoreService<Announcement, Integer> getService() {
        return announcementService;
    }

    @RequestMapping("/announcement")
    public ModelAndView announcement(){
        List list=selectAll();
        ModelAndView modelAndView = new ModelAndView("announcement");
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @RequestMapping("/announcement-vide")
    public ModelAndView announcementVide(Integer id){
        Announcement announcement=announcementService.selectByPrimaryKey(id);
        ModelAndView modelAndView = new ModelAndView("announcement-vide");
        modelAndView.addObject("announcement",announcement);
        return modelAndView;
    }

    @RequestMapping("/admin/announcement/delect")
    @ResponseBody
    public Map<String,Object> delectAnnouncement(Integer id[]){
        Boolean flag=delect(id);
        if (flag){
            return success(true,"删除成功");
        }else {
            return success(false,"删除失败");
        }

    }

    @RequestMapping("/admin/announcement/inster")
    @ResponseBody
    public Map<String,Object> instert(Announcement announcement){
        announcement.setTime(new Date());
        if (announcement.getId()!=null){
            announcementService.updata(announcement);
        }else {
            insert(announcement);
        }
        return success(true,"请求成功");
    }

    @RequestMapping("/admin/announcement/list")
    @ResponseBody
    public Map<String,Object> list(){
        List list=selectAll();
        return success(true,"查询成功",list);
    }

    @RequestMapping("/admin/announcement/getAnnouncement")
    @ResponseBody
    public Map<String,Object> getAnnouncement(Integer id){
        Announcement announcement=selectByPrimaryKey(id);
        return success(true,"请求成功",announcement);
    }

}
